package cn.bluemobi.app.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import cn.bluemobi.common.utils.MD5Tools;

/**
 * @Description webservice请求帮助类
 * @date 2014-09-25 10:52:51
 * @author 龙哲
 */
public class WebserviceUtil {

	public static void main(String[] adsf) {

		String sn = "SDK-BBX-010-27048";
		String password = "1-]4bb-4aa5";
		String pwd = sn + password;
		System.out.println(MD5Tools.encode(pwd));
	}

	/**
	 * @Description 发送get请求
	 * @param 参数列表
	 *            String urlStr 请求url地址;
	 * @return String 返回值
	 * @date 2014-7-30 下午03:08:52
	 * @author 龙哲
	 */
	public static String get(String urlStr) throws Exception {
		// url地址
		URL url = null;
		// http连接
		HttpURLConnection httpConn = null;
		// 输入流
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer();
		try {
			url = new URL(urlStr);
			httpConn = (HttpURLConnection) url.openConnection();
			// 提交模式
			httpConn.setRequestMethod("GET");
			in = new BufferedReader(new InputStreamReader(
					httpConn.getInputStream(), "UTF-8"));
			String str = null;
			while ((str = in.readLine()) != null) {
				sb.append(str);
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			in.close();
		}
		String result = sb.toString();
		return result;
	}

	/**
	 * @Description 发送post请求
	 * @param 参数列表
	 *            String urlStr 请求url地址;
	 * @return String 返回值
	 * @date 2014-7-30 下午03:08:52
	 * @author 龙哲
	 */
	public static String post(String urlStr) throws Exception {
		// url地址
		URL url = null;
		// http连接
		HttpURLConnection httpConn = null;
		// 输入流
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer();
		try {
			url = new URL(urlStr);
			httpConn = (HttpURLConnection) url.openConnection();
			// 提交模式
			httpConn.setRequestMethod("POST");
			in = new BufferedReader(new InputStreamReader(
					httpConn.getInputStream(), "UTF-8"));
			String str = null;
			while ((str = in.readLine()) != null) {
				sb.append(str);
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			in.close();
		}
		String result = sb.toString();
		return result;
	}

	/**
	 * @Description 发送post请求
	 * @param 参数列表
	 *            String urlStr 请求url地址; String paramStr 参数列表
	 *            paramData=123&name=abc
	 * 
	 * @return String 返回值
	 * @date 2014-7-30 下午03:08:52
	 * @author 龙哲
	 */
	public static String post(String urlStr, String paramStr) {
		// url地址
		URL url = null;
		// http连接
		HttpURLConnection httpConn = null;
		// 输入流
		BufferedReader in = null;
		PrintWriter out = null;
		StringBuffer sb = new StringBuffer();
		try {
			url = new URL(urlStr);
			httpConn = (HttpURLConnection) url.openConnection();
			// 提交模式
			httpConn.setRequestMethod("POST");
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			out = new PrintWriter(httpConn.getOutputStream());
			out.print(paramStr);
			out.flush();
			// OutputStreamWriter out2 = new
			// OutputStreamWriter(httpConn.getOutputStream(), "utf-8");
			// out2.write(paramStr);
			// out2.flush();
			in = new BufferedReader(new InputStreamReader(
					httpConn.getInputStream(), "UTF-8"));
			// in = new BufferedReader(new
			// InputStreamReader(httpConn.getInputStream(), "GB2312"));
			String str = null;
			while ((str = in.readLine()) != null) {
				sb.append(str);
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
			}
		}
		String result = sb.toString();
		return result;
	}

	/**
	 * @Description 发送post请求 --发送模板消息
	 * @param 参数列表
	 *            String urlStr 请求url地址; String paramStr 参数列表
	 *            paramData=123&name=abc
	 * 
	 * @return String 返回值
	 * @date 2014-7-30 下午03:08:52
	 * @author 龙哲
	 */
	public static String postTemMsg(String urlStr, String paramStr) {
		// url地址
		URL url = null;
		// http连接
		HttpURLConnection httpConn = null;
		// 输入流
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer();
		try {
			url = new URL(urlStr);
			httpConn = (HttpURLConnection) url.openConnection();
			// 提交模式
			httpConn.setRequestMethod("POST");
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			OutputStreamWriter out2 = new OutputStreamWriter(
					httpConn.getOutputStream(), "utf-8");
			out2.write(paramStr);
			out2.flush();
			in = new BufferedReader(new InputStreamReader(
					httpConn.getInputStream(), "UTF-8"));
			String str = null;
			while ((str = in.readLine()) != null) {
				sb.append(str);
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
			}
		}
		String result = sb.toString();
		return result;
	}

	// 发送短信
	public static String sendSMS(String code, String mobile) {
		String result = WebserviceUtil
				.postTemMsg("http://120.76.25.160:7788/sms.aspx",
						"action=send&" + "userid=257&"
								+ "account=zhsj&password=520620&mobile="
								+ mobile + "&content=【美好生活】您的验证码是:" + code
								+ "&sendTime=&extno=");
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			StringReader sr = new StringReader(result);
			InputSource is = new InputSource(sr);
			Document document = db.parse(is);
			Element root = document.getDocumentElement();
			NodeList nodelist1 = root.getElementsByTagName("returnstatus");
			String msgType = nodelist1.item(0).getTextContent();
			return msgType;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "Faild";
	}

}
